// pages/consultDetail/consultDetail.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tabs:[
        //   默认显示的是课程详情页面
          {id:0,name:'课程详情',isActive:true},
          {id:1,name:'课程列表',isActive:false},
          {id:2,name:'名师介绍',isActive:false},
          {id:3,name:'课程交流',isActive:false},
          {id:4,name:'课程评价',isActive:false}
      ],
      url:'',
      courseMap:{},//改为对象，动态填充数据
      currentContent:'',
      courseId:1,
      interact:'',
      lists:'',
      pageNum:1,
      pageSize:5,
      stars:[1,2,3,4,5],
      normalSrc:'/images/@2x_star_b.png',
      selectedSrc:'/images/@2x_star_y.png',
      key:1,
      courseEvaluation:''
  },
// 点击标签的处理函数
getIndex(e){
    const {index} = e.currentTarget.dataset;
    this.getContentById(index);   //点击标签时加载对应内容
},
/*
*封装课程详情接口
*/
getCourseDetail(url,id){
    return new Promise((resolve,reject)=>{
        wx.request({
          url: this.data.url+'course/'+this.data.courseId,
          method:'GET',
          success:resp=>{
              resolve(resp.data.data)
          },
          fail:err=>{
              reject(err)
          }
        })
    })
},
/*
* 根据标签显示内容
*/ 
getContentById(index){
    const { tabs,courseMap,id,url } = this.data
    // 更新标签激活状态
    tabs.forEach((tab,i) => tab.isActive = i === index)
    this.setData({tabs})

    switch(index){
        case 0:
            this.getCourseDetail(url,id).then(
                data=>{
                    courseMap[index] = data
                    this.setData({
                        courseMap,
                        currentContent:courseMap[index]
                    })
                }
            )
            break;
            case 1:
                wx.request({
                  url: this.data.url+'chapters/'+this.data.courseId,
                  method:'GET',
                  success:resp=>{
                      console.log(resp.data);
                      courseMap[index] = resp.data.data
                      this.setData({
                        courseMap,
                        lists:courseMap[index]
                      })
                  }
                })
                break;
            case 2:
                break;
            case 3:
                wx.request({
                    url: this.data.url+'classQaQuestion/page3',
                    method:"GET",
                    success:resp => {
                        courseMap[index] =resp.data.data.list;
                        this.setData({
                            courseMap,
                            interact:courseMap[index]
                        })
                    }
                })
            break;
            case 4:
                wx.request({
                    url: this.data.url+'courseRating/page3',
                    method:"GET",
                    data:{
                        pageNum:this.data.pageNum,
                        pageSize:this.data.pageSize
                    },
                    success: (resp) => {
                        console.log(resp.data.data.list)
                        courseMap[index] = resp.data.data.list
                        this.setData({
                            courseMap,
                            courseEvaluation:courseMap[index]
                        })
                    }
                  })
    }
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options.courseId){
          this.setData({
              courseId:options.courseId
            })
        }
        this.setData({
            url:app.globalData.url
          })
      this.getContentById(0)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})